package com.leyu.mall.site.search.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.leyu.mall.site.base.controller.HeaderController;
import com.leyu.search.service.GoodsSearchService;
import com.leyu.search.service.SearchQueryParam;
import com.leyu.search.service.SearchResult;

/**
 * @author change
 * 
 */
@Controller
@RequestMapping("/channel")
public class ChanelSearchController extends HeaderController{
	@Resource
	private GoodsSearchService goodsSearchService;
	private Map<String, Long> areaMapping = new HashMap<String, Long>();
	private static final CategoryView defaultCateView = new CategoryView(); 
	@RequestMapping(value = "/{area}/{resource}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String search(@PathVariable("area") String area,
			@PathVariable("resource") String resource, Model model,
			HttpServletRequest request) {
		SearchQueryParam param = this.buildQueryParam(resource);
		SearchResult result = goodsSearchService.search(param);
		model.addAttribute("searchResult", result);
		model.addAttribute("hotWords",
				goodsSearchService.getDefaultHotKeyWords());
		CategoryView cateView = this.buildCategoryView(result, param);
		model.addAttribute("cateView", cateView);
		return "search/channel_search.html";
	}

	public IdText idText(int id, String text) {
		IdText t = new IdText();
		t.id = Long.valueOf(id);
		t.text = text;
		return t;
	}

	private CategoryView createCategoryView() {
		CategoryView cateView = defaultCateView.clone();
		IdText areas[] = { idText(1, "北京"), idText(2, "上海"), idText(3, "广州"),
				idText(4, "深圳") };
		for (IdText item : areas) {
			cateView.areas.add(item);
		}
		IdText brands[] = { idText(11, "APPLE"), idText(12, "NOKIA"),
				idText(13, "SANSUNG"), idText(14, "SONY"), idText(15, "HTC"),
				idText(16, "HUAWEI"), idText(17, "ZTE"), idText(18, "COOLPAD"),
				idText(19, "LENOVO") };
		for (IdText item : brands) {
			cateView.brands.add(item);
		}
		IdText priceRanges[] = { idText(41, "1-999"), idText(42, "1000-1999"),
				idText(43, "1999-2999"), idText(44, "3000-4999"),
				idText(45, "5000以上") };
		for (IdText item : priceRanges) {
			cateView.priceRanges.add(item);
		}
		IdText systems[] = { idText(21, "IOS"), idText(22, "ANDRIOD"),
				idText(23, "WINDOWSPHONE"), idText(24, "WINDOSMOBILE"),
				idText(25, "SYMBIAN") };
		for (IdText item : systems) {
			cateView.systems.add(item);
		}
		IdText screens[] = { idText(31, "2.7英寸及一下"), idText(32, "2.8-3.4英寸"),
				idText(33, "3.5-4.0英寸"), idText(34, "4.3英寸及以上的屏幕") };
		for (IdText item : screens) {
			cateView.screens.add(item);
		}
		IdText specialTags[] = { idText(31, "双核"), idText(32, "3G视频通话"),
				idText(33, "wifi/wapi"), idText(34, "高像素手机"),
				idText(35, "导航手机"), idText(36, "商务手机"), idText(37, "女性手机"),
				idText(38, "手机电视") };
		for (IdText item : specialTags) {
			cateView.specials.add(item);
		}
		IdText sorts[] = { idText(1, "销量"), idText(2, "价格"), idText(3, "评论数"),
				idText(4, "上架时间") };
		for (IdText item : sorts) {
			cateView.sorts.add(item);
		}
		return cateView;
	}

	private CategoryView buildCategoryView(SearchResult result,
			SearchQueryParam param) {
		CategoryView cateView = createCategoryView();
		final String arrays[] = new String[SearchQueryParam.ARRAY_SIZE];
		final String srcArrays[] = param.getParamsArray();
		System.arraycopy(srcArrays, 0, arrays, 0, srcArrays.length);
		for (int i = 0; i < cateView.cateArrays.length; i++) {
			this.initCateView(arrays, 3 + i,
					(Set<IdText>) cateView.cateArrays[i]);
		}
		this.initCateView(arrays, 9, cateView.sorts);
		return cateView;
	}

	private void initCateView(String arrays[], int index, Set<IdText> idText) {
		String t = arrays[index];
		for (IdText item : idText) {
			arrays[index] = item.id + "";
			if (arrays[index].equals(t)) {
				item.selected = true;
			}
			item.href = toHref(arrays);
		}
		arrays[index] = t;
	}

	public String toHref(String arrays[]) {
		StringBuilder sb = new StringBuilder();
		for (String item : arrays) {
			if (StringUtils.isBlank(item)||"null".equalsIgnoreCase(item)) {
				sb.append("0").append("_");
			} else {
				sb.append(item).append("_");
			}

		}
		sb.deleteCharAt(sb.length() - 1).append(".html");
		return sb.toString();
	}

	@RequestMapping(value = "/{resource}", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String search(@PathVariable("resource") String resource,
			Model model, HttpServletRequest request) {
		SearchQueryParam param = this.buildQueryParam(resource);
		SearchResult result = goodsSearchService.search(param);
		model.addAttribute("searchResult", result);
		model.addAttribute("hotWords",
				goodsSearchService.getDefaultHotKeyWords());
		CategoryView cateView = this.buildCategoryView(result, param);
		model.addAttribute("cateView", cateView);
		return "search/channel_search.html";
	}

	private SearchQueryParam buildQueryParam(String resource) {
		if (StringUtils.isBlank(resource)) {
			return null;
		}
		String arrays[] = resource.split("_");
		SearchQueryParam param = new SearchQueryParam(arrays);

		
		return param;
	}

}
